<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="仓库" prop="warehouseId">
        <el-select v-model="queryParams.warehouseId" placeholder="仓库" clearable>
          <el-option
            v-for="dict in warehouseList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备类型" prop="deviceTypeId">
        <el-select v-model="queryParams.deviceTypeId" placeholder="设备类型" clearable>
          <el-option
            v-for="dict in dict.type.emg_device_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="所属单位" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入所属单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
<!--      <el-table-column label="" align="center" prop="id" v-if="true"/>-->
      <el-table-column label="仓库" align="center" prop="warehouseId">
        <template slot-scope="scope">
          <el-tag> {{ getDictWarehouseData(scope.row.warehouseId) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="设备类型" align="center" prop="deviceTypeId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.emg_device_type" :value="scope.row.deviceTypeId"/>
        </template>
      </el-table-column>
      <el-table-column label="设备名称" align="center" prop="name"/>
      <el-table-column label="操作手" align="center" prop="userId">
        <template slot-scope="scope">
          <el-tag> {{ getDictUserData(scope.row.userId) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="数量" align="center" prop="quantity"/>
      <el-table-column label="车牌号" align="center" prop="carNo"/>
      <el-table-column label="设备型号" align="center" prop="model"/>
      <el-table-column label="购买时间" align="center" prop="buyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.buyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="功率" align="center" prop="power"/>
      <el-table-column label="剩余续航时间" align="center" prop="endurance"/>
      <el-table-column label="救援能力等级" align="center" prop="rescueLevelId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.emg_device_rescue_level" :value="scope.row.rescueLevelId"/>
        </template>
      </el-table-column>
      <el-table-column label="坐标" align="center" prop="lat"/>
      <el-table-column label="坐标" align="center" prop="lng"/>
      <el-table-column label="地址" align="center" prop="address"/>
      <el-table-column label="所属单位" align="center" prop="deptId"/>
      <el-table-column label="描述" align="center" prop="remark"/>
      <el-table-column label="负责人1" align="center" prop="leader1"/>
      <el-table-column label="负责人1电话" align="center" prop="phone1"/>
      <el-table-column label="负责人2" align="center" prop="leader2"/>
      <el-table-column label="负责人2电话" align="center" prop="phone2"/>
      <el-table-column label="安全检测结果" align="center" prop="securityTest"/>
      <el-table-column label="检测结果图片" align="center" prop="securityTestImage" width="200">
        <template slot-scope="scope">
          <ImagePreview v-if="scope.row.securityTestImage"
                        :height=100
                        :src="scope.row.securityTestImage"
                        :preview-src-list="[scope.row.securityTestImage]"/>
        </template>
      </el-table-column>
      <el-table-column label="提醒检测时间" align="center" prop="remindTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.remindTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="检测合格标准" align="center" prop="passFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.emg_device_pass" :value="scope.row.passFlag"/>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改机械设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="仓库" prop="warehouseId">
          <el-select v-model="form.warehouseId" placeholder="仓库" clearable>
            <el-option
              v-for="dict in warehouseList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="设备类型" prop="deviceTypeId">
          <el-select v-model="form.deviceTypeId" placeholder="设备类型" clearable>
            <el-option
              v-for="dict in dict.type.emg_device_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="设备名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入设备名称"/>
        </el-form-item>
        <el-form-item label="操作手" prop="userId">
          <el-select v-model="form.userId" placeholder="操作手" clearable>
            <el-option
              v-for="dict in userList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入数量"/>
        </el-form-item>
        <el-form-item label="车牌号" prop="carNo">
          <el-input v-model="form.carNo" placeholder="请输入车牌号"/>
        </el-form-item>
        <el-form-item label="设备型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入设备型号"/>
        </el-form-item>
        <el-form-item label="购买时间" prop="buyTime">
          <el-date-picker clearable
                          v-model="form.buyTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择购买时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="功率" prop="power">
          <el-input v-model="form.power" placeholder="请输入功率"/>
        </el-form-item>
        <el-form-item label="剩余续航时间" prop="endurance">
          <el-input v-model="form.endurance" placeholder="请输入剩余续航时间"/>
        </el-form-item>
        <el-form-item label="救援能力等级" prop="rescueLevelId">
          <el-select v-model="form.rescueLevelId" placeholder="救援能力等级" clearable>
            <el-option
              v-for="dict in dict.type.emg_device_rescue_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="坐标" prop="lat">
          <el-input v-model="form.lat" placeholder="请输入坐标"/>
        </el-form-item>
        <el-form-item label="坐标" prop="lng">
          <el-input v-model="form.lng" placeholder="请输入坐标"/>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址"/>
        </el-form-item>
        <el-form-item label="所属单位" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入所属单位"/>
        </el-form-item>
        <el-form-item label="描述" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入描述"/>
        </el-form-item>
        <el-form-item label="负责人1" prop="leader1">
          <el-input v-model="form.leader1" placeholder="请输入负责人1"/>
        </el-form-item>
        <el-form-item label="负责人1电话" prop="phone1">
          <el-input v-model="form.phone1" placeholder="请输入负责人1电话"/>
        </el-form-item>
        <el-form-item label="负责人2" prop="leader2">
          <el-input v-model="form.leader2" placeholder="请输入负责人2"/>
        </el-form-item>
        <el-form-item label="负责人2电话" prop="phone2">
          <el-input v-model="form.phone2" placeholder="请输入负责人2电话"/>
        </el-form-item>
        <el-form-item label="安全检测结果" prop="securityTest">
          <el-input v-model="form.securityTest" placeholder="请输入安全检测结果"/>
        </el-form-item>
        <el-form-item label="检测结果图片" prop="securityTestImage">
          <singleImageUpload v-model="form.securityTestImage"/>
        </el-form-item>
        <el-form-item label="提醒检测时间" prop="remindTime">
          <el-date-picker clearable
                          v-model="form.remindTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="请选择提醒检测时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否合格" prop="passFlag">
          <el-select v-model="form.passFlag" placeholder="是否合格" clearable>
            <el-option
              v-for="dict in dict.type.emg_device_pass"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listDevice, getDevice, delDevice, addDevice, updateDevice} from "@/api/emergency/device";
import {getCustomDicts} from "@/api/system/dict/data";

export default {
  name: "Device",
  dicts: ['sys_normal_disable', 'emg_device_type', 'emg_device_rescue_level', 'emg_device_pass'],
  data() {
    return {
      warehouseList: [],
      userList: [],
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 机械设备表格数据
      deviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        warehouseId: undefined,
        deviceTypeId: undefined,
        name: undefined,
        userId: undefined,
        quantity: undefined,
        carNo: undefined,
        model: undefined,
        buyTime: undefined,
        power: undefined,
        endurance: undefined,
        rescueLevelId: undefined,
        lat: undefined,
        lng: undefined,
        address: undefined,
        deptId: undefined,
        leader1: undefined,
        phone1: undefined,
        leader2: undefined,
        phone2: undefined,
        securityTest: undefined,
        securityTestImage: undefined,
        remindTime: undefined,
        passFlag: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          {required: true, message: "不能为空", trigger: "blur"}
        ],
        warehouseId: [
          {required: true, message: "仓库ID不能为空", trigger: "blur"}
        ],
        deviceTypeId: [
          {required: true, message: "设备类型不能为空", trigger: "blur"}
        ],
        name: [
          {required: true, message: "设备名称不能为空", trigger: "blur"}
        ],
        userId: [
          {required: true, message: "操作手ID不能为空", trigger: "blur"}
        ],
        quantity: [
          {required: true, message: "数量不能为空", trigger: "blur"}
        ],
        carNo: [
          {required: true, message: "车牌号不能为空", trigger: "blur"}
        ],
        model: [
          {required: true, message: "设备型号不能为空", trigger: "blur"}
        ],
        buyTime: [
          {required: true, message: "购买时间不能为空", trigger: "blur"}
        ],
        power: [
          {required: true, message: "功率不能为空", trigger: "blur"}
        ],

        rescueLevelId: [
          {required: true, message: "救援能力等级不能为空", trigger: "blur"}
        ],

        deptId: [
          {required: true, message: "所属单位不能为空", trigger: "blur"}
        ],

        leader1: [
          {required: true, message: "负责人1不能为空", trigger: "blur"}
        ],
        phone1: [
          {required: true, message: "负责人1电话不能为空", trigger: "blur"}
        ],

      }
    };
  },
  created() {
    this.getList();
    this.getCustomDictList();
  },
  methods: {
    /** 查询机械设备列表 */
    getList() {
      this.loading = true;
      listDevice(this.queryParams).then(response => {
        this.deviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getCustomDictList() {
      this.loading = true;
      getCustomDicts("custom_warehouse_list").then(response => {
        this.warehouseList = response.data;
        this.loading = false;
      });
      getCustomDicts("custom_user_list").then(response => {
        this.userList = response.data;
        this.loading = false;
      });
    },
    getDictWarehouseData(e) {
      for (var i = 0; i < this.warehouseList.length; i++) {
        if (this.warehouseList[i].value == e) {
          return this.warehouseList[i].label;
        }
      }
    },
    getDictUserData(e) {
      for (var i = 0; i < this.userList.length; i++) {
        if (this.userList[i].value == e) {
          return this.userList[i].label;
        }
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        warehouseId: undefined,
        deviceTypeId: undefined,
        name: undefined,
        userId: undefined,
        quantity: undefined,
        carNo: undefined,
        model: undefined,
        buyTime: undefined,
        power: undefined,
        endurance: undefined,
        rescueLevelId: undefined,
        lat: undefined,
        lng: undefined,
        address: undefined,
        deptId: undefined,
        remark: undefined,
        leader1: undefined,
        phone1: undefined,
        leader2: undefined,
        phone2: undefined,
        securityTest: undefined,
        securityTestImage: undefined,
        remindTime: undefined,
        passFlag: undefined,
        createBy: undefined,
        updateBy: undefined,
        createTime: undefined,
        updateTime: undefined,
        status: 0
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加机械设备";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getDevice(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改机械设备";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateDevice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addDevice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除机械设备编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delDevice(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/device/export', {
        ...this.queryParams
      }, `device_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
